//
//  ShowTextController.m
//  TestDemo
//
//  Created by 赵博 on 2017/5/27.
//  Copyright © 2017年 赵博. All rights reserved.
//

#import "ShowTextController.h"

@interface ShowTextController ()<UITabBarDelegate,UIWebViewDelegate>
@property (nonatomic, retain) UIImageView *imgView;
@property (nonatomic, retain) UILabel *label;

@end

@implementation ShowTextController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpTabView];
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 49-64)];
    
    ///调用
    [self inView:webView];

    [self.view addSubview:webView];
    
    ///关闭下拉刷新效果
    webView.scrollView.bounces = NO;
    webView.delegate = self;
    
    
    
    
    
}
- (void)setUpTabView{
    UIView *tabView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 49, [UIScreen mainScreen].bounds.size.width, 49)];
    [self.view addSubview:tabView];
    
    for (int i = 0; i < 3; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i *[UIScreen mainScreen].bounds.size.width / 3, 0, [UIScreen mainScreen].bounds.size.width / 3, 49);
        btn.tag = 100+ i;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [tabView addSubview:btn];
    }
    
}
//从路径中获得完整的文件名 （带后缀）
//NSString *fileName = [filePath lastPathComponent];

//获得文件名 （不带后缀）
//NSString *fileName1 = [filePath stringByDeletingPathExtension];

- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"开始请求");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"结束请求");
}
-(void)inView:(UIWebView *)webView
{
    NSUserDefaults *userInfo = [NSUserDefaults standardUserDefaults];
    // 本地沙盒目录
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *tempPath = [cachesPath stringByAppendingPathComponent:@"MyTempFile"];
    NSString *pathFile = [tempPath stringByAppendingPathComponent:[userInfo objectForKey:@"tempFileName"]];
    

    NSString *suffix = [pathFile pathExtension];
    NSLog(@"---%@",suffix);
    //后缀支持视频、word、xls、ppt、pdf、图片、苹果办公工具
    if ([suffix isEqualToString:@"mp4"] || [suffix isEqualToString:@"mp3"] || [suffix isEqualToString:@"pdf"] || [suffix isEqualToString:@"wmv"]|| [suffix isEqualToString:@"jpge"] || [suffix isEqualToString:@"jpg"]|| [suffix isEqualToString:@"png"] ||  [suffix isEqualToString:@"gif"] || [suffix isEqualToString:@"txt"]|| [suffix isEqualToString:@"ppt"]||[suffix isEqualToString:@"doc"] || [suffix isEqualToString:@"pages"] || [suffix isEqualToString:@"numbers"]||[suffix isEqualToString:@"pages"] || [suffix isEqualToString:@"xls"]||[suffix isEqualToString:@"rtf"]) {
        [self.imgView removeFromSuperview];
        self.imgView = nil;
        [self.label removeFromSuperview];
        self.label = nil;
        NSURL *url = [NSURL fileURLWithPath:pathFile];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [webView loadRequest:request];
    }else{
        //不支持
        NSLog(@"不支持");
        if (self.imgView == nil) {
            self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 25, 120, 50, 50)];
            [self.imgView setImage:[UIImage imageNamed:@"type_001"]];
            [webView addSubview:self.imgView];
        }
        [self.label removeFromSuperview];
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 180,  [UIScreen mainScreen].bounds.size.width - 20, 20)];
        self.label.text = [pathFile lastPathComponent];
        [webView addSubview:self.label];
        
    }
}
#pragma mark - tab按钮点击
- (void)btnClick:(UIButton *)sender{
    
}
@end
